package com.serialcommunication.gui.component;

import static com.serialcommunication.constants.GuiConfiguration.*;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;

import com.serialcommunication.enums.Engine;
import com.serialcommunication.enums.Sign;
import com.serialcommunication.gui.frame.MainFrame;
import com.serialcommunication.interfaces.IAction;
import com.serialcommunication.interfaces.IBaseComponent;
import com.serialcommunication.interfaces.ITransmitComponent;
import com.serialcommunication.listener.MyMouseListener;

public class FunctionButton extends BaseComponent implements ITransmitComponent {
	
	private Sign sign;
	private List<ITransmitComponent> dependentComponents;
	
	private static final Color colorActive = Color.YELLOW;
	private static final Color colorInavtive = Color.GRAY;
	
	private JButton button;

	public FunctionButton(Engine pEngine, IAction pAction, MainFrame pFrame, int pSize) {
		super(pEngine, pAction, pFrame);
		
		getPanel().setLayout(null);
		button = new JButton(pAction.getTitle());
		button.setBackground(colorInavtive);
		button.setFont(new Font(Font.DIALOG, Font.PLAIN, 10));
		sign = null;
		initListener();
		dependentComponents = new ArrayList<ITransmitComponent>();
		addComponentsToPanel();
		setSizeOfElements(pSize);
		alignElementsWithinPanel();
		disableComponent();
	}
	
	public FunctionButton(Engine pEngine, IAction pAction, MainFrame pFrame, int pSize, Sign pSign) {
		super(pEngine, pAction, pFrame);
		
		getPanel().setLayout(null);
		button = new JButton(pAction.getTitle());
		button.setBackground(colorInavtive);
		button.setFont(new Font(Font.DIALOG, Font.PLAIN, 10));
		sign = pSign;
		dependentComponents = new ArrayList<ITransmitComponent>();
		initListener();
		addComponentsToPanel();
		setSizeOfElements(pSize);
		alignElementsWithinPanel();
	}
	
	public void setSizeOfElements(int pSize) {
		if (pSize == 3)  {
			getPanel().setSize(PANEL_BUTTON_SIZE_3);
			button.setSize(PANEL_BUTTON_SIZE_3);
		} else if (pSize == 2) {
			getPanel().setSize(PANEL_BUTTON_SIZE_2);
			button.setSize(PANEL_BUTTON_SIZE_2);
		}
	}
	
	public void initListener(){
		button.addMouseListener(new MyMouseListener() {
			@Override
			public void mouseReleased(MouseEvent e) {
				transmitData();
			}
		});
	}

	public void addComponentsToPanel() {
		getPanel().add(button);
	}
	
	public void alignElementsWithinPanel(){
		button.setLocation(0, 0);
	}
	
	public void addDependentComponent(ITransmitComponent comp) {
		dependentComponents.add(comp);
	}

	@Override
	public void setValue(String pValue) {
		
	}

	@Override
	public String getValue() {
		return null;
	}
	
	/**
	 * when clicked
	 */
	@Override
	public void transmitData() {
		setActive();
		for (ITransmitComponent comp : dependentComponents) {
			comp.setInActive();
		}
	}

	@Override
	public void disableComponent() {
		button.setEnabled(false);
	}

	@Override
	public void setInActive() {
		button.setBackground(colorInavtive);
	}

	@Override
	public void setActive() {
		button.setBackground(colorActive);
	}

	@Override
	public void enableComponent() {
		button.setEnabled(true);
	}
}
